package com.fplanning.planner;

import java.util.ArrayList;
import java.util.List;

import com.fplanning.planner.business.WeightSlicer;
import com.fplanning.planner.domain.CPPType;
import com.fplanning.planner.domain.FISlice;
import com.fplanning.planner.domain.FinancialPlan;
import com.fplanning.planner.domain.IFinancialInstrument;
import com.fplanning.planner.domain.RRSPType;
import com.fplanning.planner.domain.SavingsType;
import com.fplanning.planner.domain.YearlyPlan;

public class FinancialPlanGenerator
{

    public static FinancialPlan generatePlan()
    {
        FinancialPlan result = new FinancialPlan();

        RRSPType rrspType = new RRSPType();
        rrspType.setAmount(200000);

        SavingsType savings = new SavingsType();
        savings.setAmount(23000);
        
        CPPType cpp = new CPPType();
        cpp.setAmount(12000);

        List<IFinancialInstrument> financialInstrumentList = new ArrayList<IFinancialInstrument>();
        financialInstrumentList.add(rrspType);
        financialInstrumentList.add(savings);
        financialInstrumentList.add(cpp);

        result.setFinancialInstrumentList(financialInstrumentList);
        result.setFiSlicesList(createSlices(financialInstrumentList));

        List<YearlyPlan> yearlyPlanList = new ArrayList<YearlyPlan>();

        YearlyPlan year1 = new YearlyPlan();
        year1.setId(1);
        year1.setRequiredAmount(45000);
        yearlyPlanList.add(year1);

        YearlyPlan year2 = new YearlyPlan();
        year2.setId(2);
        year2.setRequiredAmount(36000);
        yearlyPlanList.add(year2);

        YearlyPlan year3 = new YearlyPlan();
        year3.setId(3);
        year3.setRequiredAmount(55000);
        yearlyPlanList.add(year3);
        
        result.setYearlyPlanList(yearlyPlanList);

        return result;
    }

    public static List<List<FISlice>> createSlices( List<IFinancialInstrument> financialInstrumentList)
    {
        List<List<FISlice>> result = new ArrayList<List<FISlice>>();
        
        List<IFinancialInstrument> variableFis = new ArrayList<IFinancialInstrument>();
        List<IFinancialInstrument> fixedFis = new ArrayList<IFinancialInstrument>();
        
        for (IFinancialInstrument iFinancialInstrument : financialInstrumentList)
        {
            if(iFinancialInstrument != null && iFinancialInstrument.isAmountVariable())
            {
                variableFis.add(iFinancialInstrument);
            }
            else
            {
                fixedFis.add(iFinancialInstrument);
            }
        }
        
        WeightSlicer slicer = new WeightSlicer(10, 100, variableFis.size());
        List<List<Double>> l = slicer.getCombos();
        for (int i = 0; i < l.get(0).size(); i++)
        {
            List<FISlice> slices = new ArrayList<FISlice>();
            result.add(slices);
            System.out.print(i + ": ");
            for (int j = 0; j < l.size(); j++)
            {
                FISlice slice = new FISlice();
                slice.setfInstrument(variableFis.get(j));
                slice.setWeightPct(l.get(j).get(i));
                slices.add(slice);
                System.out.print(l.get(j).get(i) + "\t");
            }
            System.out.println();
            for (IFinancialInstrument iFinancialInstrument : fixedFis)
            {
                FISlice slice = new FISlice();
                slice.setfInstrument(iFinancialInstrument);
                slice.setWeightPct(100);
                slices.add(slice);
            }
            
        }
        
//      for (int i = 0; i <= 100; i = i + 10)
//      {
//          FISlice rrspSlice = new FISlice();
//          rrspSlice.setWeightPct(i);
//          rrspSlice.setfInstrument(rrsp);
//
//          FISlice savingsSlice = new FISlice();
//          savingsSlice.setWeightPct(100 - i);
//          savingsSlice.setfInstrument(savings);
//
//          List<FISlice> slices = new ArrayList<FISlice>();
//          slices.add(rrspSlice);
//          slices.add(savingsSlice);
//
//          result.add(slices);
//      }
        return result;
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

    }

}
